package org.road0329.dlt698.axdr;

import org.road0329.dlt698.ReverseByteArrayInputStream;
import org.road0329.dlt698.ReverseByteArrayOutputStream;
import org.road0329.dlt698.asn1.axdr.AxdrEnum;
import org.road0329.dlt698.asn1.axdr.AxdrType;

import java.io.IOException;
import java.nio.ByteBuffer;
public class Action_Response extends AxdrType {
    private ByteBuffer dataCode = null;
    private Choice choice = Choice._ERR_NONE_SELECTED;
    private ActionResponseNormal actionResponseNormal = null;
    private ActionResponseNormalList actionResponseNormalList = null;
    private ActionThenGetResponseNormalList actionThenGetResponseNormalList = null;

    public Action_Response() {
    }

    public ByteBuffer getDataCode() {
        return dataCode;
    }

    public ActionResponseNormal getActionResponseNormal() {
        return actionResponseNormal;
    }

    public ActionResponseNormalList getActionResponseNormalList() {
        return actionResponseNormalList;
    }

    public ActionThenGetResponseNormalList getActionThenGetResponseNormalList() {
        return actionThenGetResponseNormalList;
    }

    public void set_dataCode(ByteBuffer dataCode) {
        this.dataCode = dataCode;
    }

    public void setActionResponse(ActionResponseNormal actionResponseNormal) {
        resetChoices();
        this.choice = Choice.ACTION_RESPONSE_NORMAL;
        this.actionResponseNormal = actionResponseNormal;
    }

    public void setActionResponseList(ActionResponseNormalList actionResponseNormalList) {
        resetChoices();
        this.choice = Choice.ACTION_RESPONSE_NORMAL_LIST;
        this.actionResponseNormalList = actionResponseNormalList;
    }

    public void setActionThenGetResponseNormalList(ActionThenGetResponseNormalList actionThenGetResponseNormalList) {
        resetChoices();
        this.choice = Choice.ACTION_THEN_GET_RESPONSE_NORMAL_LIST;
        this.actionThenGetResponseNormalList = actionThenGetResponseNormalList;
    }

    public int decode(ReverseByteArrayInputStream input) throws Exception {
        int codeLength = 0;
        AxdrEnum choosen = new AxdrEnum();
        choosen.setConst();
        codeLength += choosen.decode(input);
        resetChoices();
        this.choice = Choice.getEnumByValue((int)choosen.getValue());
        if (this.choice == Choice.ACTION_RESPONSE_NORMAL) {
            this.actionResponseNormal = new ActionResponseNormal();
            codeLength += this.actionResponseNormal.decode(input);
            return codeLength;
        } else if (this.choice == Choice.ACTION_RESPONSE_NORMAL_LIST) {
            this.actionResponseNormalList = new ActionResponseNormalList();
            codeLength += this.actionResponseNormalList.decode(input);
            return codeLength;
        } else if (this.choice == Choice.ACTION_THEN_GET_RESPONSE_NORMAL_LIST) {
            this.actionThenGetResponseNormalList = new ActionThenGetResponseNormalList();
            codeLength += this.actionThenGetResponseNormalList.decode(input);
            return codeLength;
        }
        throw new Exception("Error decoding AxdrChoice: Identifier matched to no item.");
    }

    public int encode(ReverseByteArrayOutputStream output) throws Exception {
        if (this.dataCode != null) {
            byte[] dataCodeArray = this.dataCode.array();
            for (int i = dataCodeArray.length - 1; i >= 0; i--) {
                output.write(dataCodeArray[i]);
            }
            return dataCodeArray.length;
        }
        if (this.choice == Choice._ERR_NONE_SELECTED) {
            throw new Exception("Error encoding AxdrChoice: No item in choice was selected.");
        }
        int codeLength = 0;
        if (this.choice == Choice.ACTION_THEN_GET_RESPONSE_NORMAL_LIST) {
            codeLength += this.actionThenGetResponseNormalList.encode(output);
            AxdrEnum c = new AxdrEnum();
            c.setMinMaxVal(3);
            codeLength += c.encode(output);
            return codeLength;
        } else if (this.choice == Choice.ACTION_RESPONSE_NORMAL_LIST) {
            codeLength += this.actionResponseNormalList.encode(output);
            AxdrEnum c = new AxdrEnum();
            c.setMinMaxVal(2);
            codeLength += c.encode(output);
            return codeLength;
        } else if (this.choice == Choice.ACTION_RESPONSE_NORMAL) {
            codeLength += this.actionResponseNormal.encode(output);
            AxdrEnum c = new AxdrEnum();
            c.setMinMaxVal(1);
            codeLength += c.encode(output);
            return codeLength;
        }
        throw new Exception("Error encoding AxdrChoice: No item in choice was encoded.");
    }

    public Choice getChoiceIndex() {
        return this.choice;
    }

    public void resetChoices() {
        this.choice = Choice._ERR_NONE_SELECTED;
        this.actionResponseNormal = null;
        this.actionResponseNormalList = null;
        this.actionThenGetResponseNormalList = null;
    }

    public void encodeAndSave(int encodingSizeGuess) throws Exception {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        encode(revOStream);
        this.dataCode = ByteBuffer.wrap(revOStream.getArray());
    }

    public String toString() {
        if (this.choice == Choice.ACTION_RESPONSE_NORMAL) {
            if (this.actionResponseNormal != null) {
                return "choice: {actionResponseNormal: " + this.actionResponseNormal + "}";
            } else {
                return "choice is actionResponseNormal but actionResponseNormal is null";
            }
        } else if (this.choice == Choice.ACTION_RESPONSE_NORMAL_LIST) {
            if (this.actionResponseNormalList != null) {
                return "choice: {actionResponseNormalList: " + this.actionResponseNormalList + "}";
            } else {
                return "choice is actionResponseNormalList but actionResponseNormalList is null";
            }
        } else if (this.choice == Choice.ACTION_THEN_GET_RESPONSE_NORMAL_LIST) {
            if (this.actionThenGetResponseNormalList != null) {
                return "choice: {actionThenGetResponseNormalList: " + this.actionThenGetResponseNormalList + "}";
            } else {
                return "choice is actionThenGetResponseNormalList but actionThenGetResponseNormalList is null";
            }
        } else {
            return "unknown";
        }
    }

    enum Choice {
        _ERR_NONE_SELECTED(-1),
        ACTION_RESPONSE_NORMAL(1),
        ACTION_RESPONSE_NORMAL_LIST(2),
        ACTION_THEN_GET_RESPONSE_NORMAL_LIST(3);

        private final int value;

        private Choice(int value) {
            this.value = value;
        }

        public int getValue() {
            return value;
        }

        public static Choice getEnumByValue(int value) {
            for (Choice item : Choice.values()) {
                if (value == item.getValue()) {
                    return item;
                }
            }
            return null;
        }
    }
}
